document.addEventListener('DOMContentLoaded', () => {
    // 获取DOM元素
    const navbarToggle = document.querySelector('.navbar-toggle');
    const navbarMenu = document.querySelector('.navbar-menu');

    // 添加汉堡菜单点击事件
    if (navbarToggle && navbarMenu) {
        navbarToggle.addEventListener('click', () => {
            navbarToggle.classList.toggle('active');
            navbarMenu.classList.toggle('active');
            
            // 更新aria-expanded状态
            const isExpanded = navbarToggle.classList.contains('active');
            navbarToggle.setAttribute('aria-expanded', isExpanded);
        });
    }

    // 添加窗口大小变化事件
    let resizeTimer;
    window.addEventListener('resize', () => {
        // 防抖处理
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(() => {
            // 如果窗口宽度大于768px，重置菜单状态
            if (window.innerWidth > 768) {
                navbarToggle?.classList.remove('active');
                navbarMenu?.classList.remove('active');
                navbarToggle?.setAttribute('aria-expanded', 'false');
            }
        }, 100);
    });

    // 点击导航链接时关闭菜单
    const navLinks = document.querySelectorAll('.nav-links a');
    navLinks.forEach(link => {
        link.addEventListener('click', () => {
            if (window.innerWidth <= 768) {
                navbarToggle?.classList.remove('active');
                navbarMenu?.classList.remove('active');
                navbarToggle?.setAttribute('aria-expanded', 'false');
            }
        });
    });

    // 添加键盘导航支持
    navbarToggle?.addEventListener('keydown', (e) => {
        if (e.key === 'Enter' || e.key === ' ') {
            e.preventDefault();
            navbarToggle.click();
        }
    });
});
